#ifndef PINPAD_H
#define PINPAD_H

#include "Global.h"


/* Definitions for sizes */
#define SEND_BUF_SIZE 256
#define RECV_BUF_SIZE 256
/* Definitions for various error types */
#define ERR_COM_OPEN                    (-10) /* port open failed                   */
#define ERR_COM_INIT                    (-11) /* Error setting port parameters      */
#define ERR_COM_CLOSE                   (-12) /* port close failed                  */
#define ERR_COM_SEND                    (-13) /* Write to Com port failed           */
#define ERR_COM_NAK                     (-14) /* Did not receive ACK for last write */
#define ERR_COM_READ                    (-15) /* Error setting port parameters      */
#define ERR_COM_WRITE                   (-16) /* Error setting port parameters      */
#define ERR_COM_TIMEOUT                 (-17) /* Error setting port parameters      */
#define ERR_COM_RECV                    (-18) /* Error setting port parameters      */



#define NO_PINPAD   0 
#define PP101       1   /* Supports Master/Session */
#define PP1000      2   /* Supports Master/Session + DUKPT + MAC'ing */
#define PP102       3   /* Supports DUKPT */
#define IPP         4   /* Supports Master/Session + DUKPT + MAC'ing */
#define PP501       5   /* Supports Master/Session + DUKPT + MAC'ing */
#define SC552       6   /* PinPad + 2 SmartCard Reader/Writers + MSR */
#define PP201       7   /* Supports Master/Session + MSR */
#define PP2000      8   /* Supports Master/Session + DUKPT + MAC'ing + MSR */
#define SC455       9   /* PinPad + SmartCard Reader/Writer, supports Master/Session + DUKPT */

/* Encryption Method */
#define MASTER_KEY  0
#define DUKPT       1

#define BPI_MAC             0       /* BPI alogorithm  */
#define ANSI_MAC            1       /* ANSI algorithm */
                            
#define ASCII               0
#define BINARY              1

#define PP_MAX_RETRIES      3       /* Maximum number of retries */
#define PINPAD_BUF_SIZE     250     /* Max size for PP RX and TX buffers */

/* PINpad response codes */
#define PP_SUCCESS          0       
#define PP_ERROR            1
#define PP_TIMEOUT          2
#define PP_CANCEL           3
#define PP_CORRECT          4
#define PP_BADBALANCE       5
#define PP_BADACCTLEN       6
#define PP_MACFAILED        7
#define PP_NOMACBLOCK       9
#define PP_BADDECRYPTION    11
#define PP_NEWKEYSFAILED    16
#define PP_CANNOTREVERTKEYS 98
#define PP_SC_FAILED        99

#define PPADTIMEOUT         6
#define PPAD501TIMEOUT      12

#define PINPAD_PIN_RESP_LEN 27      /* Normal length of resp pkt for PIN */
#define PINPAD_BUFFER_SIZE  150
#define LAST                0       /* Used in MACing */
#define TIMEOUT_DELAY       10      /* Timeout needed for PinPad */

#define CLR                 0x1A    /* Clear the pinpad screen or not */
#define MAX_ACT_RETRYS      3       /* Account Entry Max retries */

/* Format flags 0-4 with Fmt_Auto setting */
#define Fmt_A7E1_auto       10
#define Fmt_A7N1_auto       11
#define Fmt_A7O1_auto       12
#define Fmt_A8E1_auto       13
#define Fmt_A8N1_auto       14

#define PIN_BLOCK_SIZE           16

#define PINPAD_MESSAGE_FILE "pin"

#define strlcpy(a,b,c) strcpy(a,b)
#define GET_VER(a,b,c,d,e)  unsigned char tmp[10];strcpy((char *)tmp,"1234");

/* Structure passed as input for PIN Encryption */
typedef struct tagPinInfoStruct
{
    char pchPrimaryAcctInfo[19+1];
    char pchDispStr1[16+1];
    char pchDispStr2[16+1];
    char pchEncryptionKey[16+1];
    char pchMaxPIN[2+1];
    char pchMinPIN[2+1];
    int  inKeySlotPos;
    char pchDispOver[16+1];
    char pchEncryptedPIN[16+1];
} PININFOSTRUCT;


typedef struct tagPinPadObject
{
    int inHandle;
    int inType;
    int inTimeout;      /* PPADTIMEOUT, PPAD501TIMEOUT */

    /* Function pointers for object */
    int (*inInitPinPad)(void);
    int (*inSetPinPadIdlePrompt)(void);
    int (*inDisplayPinPadString)(char chClrDisplay, char *chDispString);
    int (*inGetPin)(TRANSACTION_OBJECT *pobTran, char *Msg);
    int (*inGetTipAmount)(TRANSACTION_OBJECT *pobTran);
    int (*inPPadConnectTest)(void);
    void(*vdSendAbortToPINPad)(void);
    
} PINPAD_OBJECT;

int  inColdInitPinpad(TRANSACTION_OBJECT *pobTran);
int inGetPinAccMac(TRANSACTION_OBJECT *pobTran, char *PINMsg);

int  inSetupPinPad(int inPinPadType);
int  inSetupIPPInfo(int inPinPadType);
int  inInitializePinPad(void);
int  inExecPinPadIdlePrompt(void);

int  inInitPinPad(void);
int  inSetIPPIdlePrompt(void);
int  inDispIPPStr(char chClrDisplay, char *pszDispStr);
int  inGetIPPPin(TRANSACTION_OBJECT *pobTran, char *MsgPin);
int  inGetIPPTipAmt(TRANSACTION_OBJECT *pobTran);
int  inPPadConnectTest(void);
void vdSendAbortToPINPad(void);

int  inSendPinPadData(char *pchMsg, int inTimeOut, VS_BOOL fWaitForAck);
int  inRecvPinPadData(char *pchRecBuf, int inTimeOut, int inMaxRecSize, VS_BOOL fSendAck);
int  inResetPINPad(void);
int  inSelectMasterKey(void);
int  inTransferMasterKey(char MKAddress, char *szMasterKey);
void vdDispPinPadPkt73Error(char chError);
int  PINPAD_CODE_CHECK(char *a,char *b,int *c,char *d,unsigned char *e);
void vdUpperCasePinMsg(char *pchString);

int inSetKeyManagement(void);  //from pp1000.h

int inSendComPacket(int inHandle, char *pchMsg, int inMsgSize, VS_BOOL fPadStxEtx, 
                    int inACKTimeOut, VS_BOOL fWaitForAck, int inMaxRetries);  //from Rs232.h
int inRecvCModePacket(int inHandle, char *pchRecBuf, int inMaxRecSize, VS_BOOL fStripStxEtx, 
                      int inRespTimeOut, VS_BOOL fSendAck, int inMaxRetries);


//LIEM : Redefinisi Function
int inGetPINPadType(void);
char chGetMasterKeyAddress(void);
int inGetPINKey(char *buff);
int inGetEncryptMethod(void);
int inGetMinPINDigit(void);
int inGetMaxPINDigit(void);
int inGetPPTimeOut(void);
int inGetPinBarBaudRate(void);
int inGetPinBarDataFormat(void);

//PINPAD TELLER MODULE
int inEnableFrontEndPinpad(void);
int inDoPinpadRecvProcessSend(void);
int	inCloseComPort(void);
int	inInitComPortForPinpad(void);
int inReadComPort(char *Recv, int inTimeOut);
int inWriteComPort(char *Sent, int inSentSize, int inTimeOut);
int inSelectPinpadMasterKey();
int inGetPinForPinpad(char *szEPin, char *label);
int inGetCardForPinpad(char *szTrack2, char *szPAN);
int inCheckCardForPinpad(char *szPAN, char *szReqBuff);
void vdDisplayPinpadStatus(char *szCode);


#define HOST_PINPAD   "HOST.PINPAD"
#define HOST_PINKEY   "HOST.PINKEY"
#endif